package cn.unionship.osc_unionship.utils;

import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

/**
 * Created by LG on 2015/9/14.
 */
public class DateFormat {

    /**
     *
     * 获取现在时间
     *
     * @return 返回时间类型 yyyy-MM-dd HH:mm:ss
     */

    public static Date getNowDate() {

        Date currentTime = new Date();

        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        String dateString = formatter.format(currentTime);

        ParsePosition pos = new ParsePosition(8);

        Date currentTime_2 = formatter.parse(dateString, pos);

        return currentTime_2;

    }

    /**
     *
     * 获取现在时间
     *
     * @return返回短时间格式 yyyy-MM-dd
     */

    public static Date getNowDateShort() {

        Date currentTime = new Date();

        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");

        String dateString = formatter.format(currentTime);

        ParsePosition pos = new ParsePosition(8);

        Date currentTime_2 = formatter.parse(dateString, pos);

        return currentTime_2;

    }

    /**
     *  long型时间转为yyyy_MM_dd_hh_mm_ss字符串
     * @param timess
     * @return
     */
    public static String longdateTo_yyyy_MM_dd_hh_mm_ss(Long timess) {

        java.util.Date dt = new Date(timess);

        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        String dateString = formatter.format(dt);

        return dateString;

    }

    /**
     *
     * 获取现在时间
     *
     * @return返回字符串格式 yyyy-MM-dd HH:mm:ss
     */

    public static String getStringDate() {

        Date currentTime = new Date();

        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        String dateString = formatter.format(currentTime);

        return dateString;

    }

    /**
     *
     * 获取现在时间
     *
     * @return 返回短时间字符串格式yyyy-MM-dd
     */

    public static String getStringDateShort() {

        Date currentTime = new Date();

        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");

        String dateString = formatter.format(currentTime);

        return dateString;

    }

    /**
     *
     * 获取时间 小时:分;秒 HH:mm:ss
     *
     * @return
     */

    public static String getTimeShort() {

        SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");

        Date currentTime = new Date();

        String dateString = formatter.format(currentTime);

        return dateString;

    }

    /**
     *
     * 将长时间格式字符串转换为时间 yyyy-MM-dd HH:mm:ss
     *
     */

    public static Date strToDateLong(String strDate) {

        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        ParsePosition pos = new ParsePosition(0);

        Date strtodate = formatter.parse(strDate, pos);

        return strtodate;

    }


    /**
     *
     * 将长时间格式字符串转换为时间 yyyy-MM-dd HH:mm:ss
     *
     */

    public static String strToString_yyyy_MM_dd_HH_mm(String strDate) {

        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        ParsePosition pos = new ParsePosition(0);
        Date strtodate = formatter.parse(strDate, pos);
        SimpleDateFormat formatter2 = new SimpleDateFormat("yyyy-MM-dd HH:mm");

        String dateString = formatter2.format(strtodate);

        return dateString;


    }

    /** * 将长时间格式时间转换为字符串 yyyy-MM-dd HH:mm:ss * * @param dateDate * @return */

    public static String dateToStrLong(java.util.Date dateDate) {

        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        String dateString = formatter.format(dateDate);

        return dateString;

    }

    /**
     * 将短时间格式时间转换为字符串 yyyy-MM-dd
     */

    public static String dateToStr(java.util.Date dateDate) {

        // SimpleDateFormat formatter = new SimpleDateFormat("yyyy年MM月dd日");
        // SimpleDateFormat formatter = new SimpleDateFormat("MM月dd日");
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");

        String dateString = formatter.format(dateDate);

        return dateString;

    }

    /**
     * 将短时间格式时间转换为字符串 MM月dd日
     */

    public static String dateToStr_MM_dd(java.util.Date dateDate) {

        // SimpleDateFormat formatter = new SimpleDateFormat("yyyy年MM月dd日");
        // SimpleDateFormat formatter = new SimpleDateFormat("MM月dd日");
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");

        String dateString = formatter.format(dateDate);

        return dateString;

    }

    /**
     * 将短时间格式时间转换为字符串 MM_dd_hh_mm
     */

    public static String dateTo_MM_dd_hh_mm(Date dateDate) {

        // SimpleDateFormat formatter = new SimpleDateFormat("yyyy年MM月dd日");
        // SimpleDateFormat formatter = new SimpleDateFormat("MM月dd日");
        String dateString = "";
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        if (dateDate != null) {
            dateString = formatter.format(dateDate);
        }

        return dateString;

    }

    /**
     * 将短时间格式时间转换为字符串 yyyy_MM_dd_hh_mm
     */

    public static String dateTo_mm_dd_hh_mm(Date dateDate) {

        // SimpleDateFormat formatter = new SimpleDateFormat("yyyy年MM月dd日");
        // SimpleDateFormat formatter = new SimpleDateFormat("MM月dd日");
        String dateString = "";
        SimpleDateFormat formatter = new SimpleDateFormat("MM-dd HH:mm");
        if (dateDate != null) {
            dateString = formatter.format(dateDate);
        }

        return dateString;

    }

    /**
     * 将短时间格式时间转换为字符串 yyyy_MM_dd_hh_mm
     */

    public static String dateTo_mm_a_dd_hh_mm(Date dateDate) {

        // SimpleDateFormat formatter = new SimpleDateFormat("yyyy年MM月dd日");
        // SimpleDateFormat formatter = new SimpleDateFormat("MM月dd日");
        String dateString = "";
        SimpleDateFormat formatter = new SimpleDateFormat("MM-dd a HH:mm");
        if (dateDate != null) {
            dateString = formatter.format(dateDate);
        }

        return dateString;

    }

    /**
     * 将短时间格式时间转换为字符串 yyyy_MM_dd_hh_mm
     */

    public static String dateTo_yyyy_MM_dd_hh_mm(Date dateDate) {

        // SimpleDateFormat formatter = new SimpleDateFormat("yyyy年MM月dd日");
        // SimpleDateFormat formatter = new SimpleDateFormat("MM月dd日");
        String dateString = "";
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        if (dateDate != null) {
            dateString = formatter.format(dateDate);
        }

        return dateString;

    }

    /**
     * 将短时间格式时间转换为字符串 MM_dd 上下午 hh_mm
     */

    public static String dateTo_MM_dd_a_hh_mm(Date dateDate) {

        // SimpleDateFormat formatter = new SimpleDateFormat("yyyy年MM月dd日");
        // SimpleDateFormat formatter = new SimpleDateFormat("MM月dd日");
        String dateString = "";
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd a HH:mm");
        if (dateDate != null) {
            dateString = formatter.format(dateDate);
        }

        return dateString;

    }


    /**
     * 将短时间格式时间转换为字符串yyyy_ MM_dd + 时间段
     */

    public static String dateTo_YYYY_MM_dd_timeStr(Date dateDate,String timeStr) {

        // SimpleDateFormat formatter = new SimpleDateFormat("yyyy年MM月dd日");
        // SimpleDateFormat formatter = new SimpleDateFormat("MM月dd日");
        String dateString = "";
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd ");
        if (dateDate != null) {
            dateString = formatter.format(dateDate)+timeStr;
        }

        return dateString;

    }


    /**
     * 将短时间格式时间转换为字符串 yyyy_MM_dd_hh_mm_ss
     */

    public static String dateTo_yyyy_MM_dd_hh_mm_ss(Date dateDate) {

        // SimpleDateFormat formatter = new SimpleDateFormat("yyyy年MM月dd日");
        // SimpleDateFormat formatter = new SimpleDateFormat("MM月dd日");
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateString = "";
        if (dateDate != null) {
            dateString = formatter.format(dateDate);
        }

        return dateString;

    }

    /**
     * 将短时间格式时间转换为字符串 yyyy_MM_dd
     */

    public static String dateTo_yyyy_MM_dd(Long timess) {

        // SimpleDateFormat formatter = new SimpleDateFormat("yyyy年MM月dd日");
        // SimpleDateFormat formatter = new SimpleDateFormat("MM月dd日");

        // 前面的lSysTime是秒数，先乘1000得到毫秒数，再转为java.util.Date类型
        java.util.Date dt = new Date(timess);

        SimpleDateFormat formatter = new SimpleDateFormat("yyyy年MM月dd日 ");

        String dateString = formatter.format(dt);

        return dateString;

    }

    /**
     * 将短时间格式时间转换为字符串 yyyy_MM_dd
     */

    public static String dateTo_MM_dd_hh_mm_diff(Date date) {

        // SimpleDateFormat formatter = new SimpleDateFormat("yyyy年MM月dd日");
        // SimpleDateFormat formatter = new SimpleDateFormat("MM月dd日");

        // 前面的lSysTime是秒数，先乘1000得到毫秒数，再转为java.util.Date类型
        SimpleDateFormat formatter = new SimpleDateFormat("MM月dd日 HH时mm分");

        String dateString = formatter.format(date);

        return dateString;

    }

    /**
     * 将短时间格式时间转换为字符串 yyyy_MM_dd
     */

    public static String dateTo_yyyy_MM_dd(Date dateDate) {

        // SimpleDateFormat formatter = new SimpleDateFormat("yyyy年MM月dd日");
        // SimpleDateFormat formatter = new SimpleDateFormat("MM月dd日");
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy年MM月dd日 ");

        String dateString = formatter.format(dateDate);

        return dateString;

    }

    /**
     * 将短时间格式时间转换为字符串 yyyy-MM-dd
     */

    public static String dateToStr(Date dateDate, String format) {

        // SimpleDateFormat formatter = new SimpleDateFormat("yyyy年MM月dd日");
        // SimpleDateFormat formatter = new SimpleDateFormat("MM月dd日");
        SimpleDateFormat formatter = new SimpleDateFormat(format);
        String dateString = formatter.format(dateDate);
        return dateString;

    }

    /**
     * 将短时间格式字符串转换为时间 yyyy-MM-dd
     */

    public static Date strToDate(String strDate) {

        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");

        ParsePosition pos = new ParsePosition(0);

        Date strtodate = null;
        try {
            strtodate = formatter.parse(strDate, pos);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return strtodate;

    }

    /**
     *
     * 得到现在时间
     */

    public static Date getNow() {

        Date currentTime = new Date();

        return currentTime;

    }

    /**
     *
     * 提取一个月中的最后一天
     */

    public static Date getLastDate(long day) {

        Date date = new Date();

        long date_3_hm = date.getTime() - 3600000 * 34 * day;

        Date date_3_hm_date = new Date(date_3_hm);

        return date_3_hm_date;

    }

    /**
     *
     * 得到现在时间
     */

    public static String getStringToday() {

        Date currentTime = new Date();

        SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd HHmmss");

        String dateString = formatter.format(currentTime);

        return dateString;

    }

    /**
     *
     * 得到现在小时
     */

    public static String getHour() {

        Date currentTime = new Date();

        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        String dateString = formatter.format(currentTime);

        String hour;

        hour = dateString.substring(11, 13);

        return hour;

    }


    public static ArrayList<String> getThreeDate(){
        ArrayList<String> dates=new ArrayList<String>();
        java.text.SimpleDateFormat   df=new   java.text.SimpleDateFormat( "yyyy-MM-dd ");
        java.util.Calendar   calendar=java.util.Calendar.getInstance();
        calendar=java.util.Calendar.getInstance();
        dates.add(df.format(calendar.getTime()));//今天
        calendar.add(java.util.Calendar.DAY_OF_YEAR,1);
        dates.add(df.format(calendar.getTime()));//明天
        calendar.add(java.util.Calendar.DAY_OF_YEAR,1);
        dates.add(df.format(calendar.getTime()));//后天
        return dates;
    }


    public static String timeDiff(long publishTime) {
        StringBuilder timeDiff = new StringBuilder();
        try {
            // SimpleDateFormat df = newSimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            // java.util.Date now = df.parse("2004-03-26 13:31:40");
            // java.util.Date date=df.parse("2004-01-02 11:30:24");
            Date now = new Date();
            long l = now.getTime() - publishTime;
            long day = l / (24 * 60 * 60 * 1000);
            long hour = (l / (60 * 60 * 1000) - day * 24);
            long min = ((l / (60 * 1000)) - day * 24 * 60 - hour * 60);
            long s = (l / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
            System.out.println("" + day + "天" + hour + "小时" + min + "分" + s
                    + "秒");
            if (day != 0) {
                timeDiff.append(day + "天");
            }
            if (hour != 0) {
                timeDiff.append(hour + "小时");
            }
            if (min != 0) {
                timeDiff.append(min + "分");
            }
            if (timeDiff.length() == 0) {
                timeDiff.append(s + "秒");
            }
            timeDiff.append("之前");

        } catch (Exception e) {
            e.printStackTrace();
        }
        return timeDiff.toString();
    }

    public static String timeDiff(Date publishTime) {
        StringBuilder timeDiff = new StringBuilder();
        try {
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            // java.util.Date now = df.parse("2004-03-26 13:31:40");
            // java.util.Date date=df.parse("2004-01-02 11:30:24");
            Date now = new Date();
            long l = now.getTime() - publishTime.getTime();
            long year = l / (365 * 24 * 60 * 60 * 1000);
            long day = l / (24 * 60 * 60 * 1000);
            long hour = (l / (60 * 60 * 1000) - day * 24);
            long min = ((l / (60 * 1000)) - day * 24 * 60 - hour * 60);
            long s = (l / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);

            if (year >= 1) {
                timeDiff.append(dateTo_yyyy_MM_dd(publishTime));
            } else if (day >= 2) {
                timeDiff.append(dateTo_MM_dd_hh_mm_diff(publishTime));
				/*
				 * if (day != 0) { timeDiff.append(day + "天"); } if (hour != 0)
				 * { timeDiff.append(hour + "小时"); } if (min != 0) {
				 * timeDiff.append(min + "分"); } if (timeDiff.length() == 0) {
				 * timeDiff.append(s + "秒"); } timeDiff.append("之前");
				 */
            } else if (day >= 1) {
                timeDiff.append("昨天");
            } else if (hour >= 1) {
                timeDiff.append(hour + "小时前");
            } else if (min >= 1) {
                timeDiff.append(min + "分钟前");
            } else {
                timeDiff.append("刚刚");
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return timeDiff.toString();
    }

    public static long getBetweenYears(String birthday) {
        long betweenTime = 0;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd");
        try {
            Date date = sdf.parse(birthday);// 通过日期格式的parse()方法将字符串转换成日期 Date
            // dateBegin = sdf.parse(date2);
            betweenTime = new Date().getTime() - date.getTime();
            betweenTime = betweenTime / 1000 / 60 / 60 / 24 / 365;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return betweenTime;
    }

}
